﻿'Clinciu Andrei
Option Strict On
Imports Gift.BL

Public Class frmProductExport
    Private _product As Product
    Public Occasions As List(Of Occasion)
    Public isNew As Boolean = True

    Public Property Product As Product
        Get
            _product.Naam = txtNaam.Text
            _product.Omschrijving = txtOmschrijving.Text
            _product.Prijs = CDec(txtPrijs.Text.Replace(",", "."))
            _product.Afbeelding = txtAfbeelding.Text
            _product.Onbeschikbaar = chkOnbeschikbaar.Checked
            Console.WriteLine(_product.Omschrijving)
            Return _product
        End Get
        Set(ByVal Value As Product)
            _product = Value
            txtNaam.Text = _product.Naam
            txtOmschrijving.Text = _product.Omschrijving
            txtPrijs.Text = CStr(_product.Prijs)
            txtAfbeelding.Text = _product.Afbeelding
            chkOnbeschikbaar.Checked = _product.Onbeschikbaar

        End Set
    End Property

    Public Function GetOccasions() As List(Of Occasion)
        Dim theList As New List(Of Occasion)
        ' Console.WriteLine("selected the following items..")
        For Each chk As Occasion In chkGelegenheid.CheckedItems
            theList.Add(chk)
            '  Console.WriteLine(chk)
        Next
        Return theList
    End Function

#Region "Events Handling"
    Private Sub frmProductEditAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Misc settings for dialogs
        EnableDisableControl()
        LoadOcasions()
    End Sub


    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        'use the ok button for the dialog
        Dim isOk As Boolean = True
        If Product.ExistsName(Product.Naam, frmMain.dbh) And isNew Then
            errProvider.SetError(txtNaam, "Er is al een product met deze naam in het database!")
            ' MessageBox.Show("Er is al een product met deze naam in het database!", "FOUT", MessageBoxButtons.OK, MessageBoxIcon.Error)
            '  isOk = False
        Else
            errProvider.SetError(txtNaam, "")
        End If
        If GetOccasions().Count = 0 Then
            errProvider.SetError(chkGelegenheid, "Je moet minstens een gelegenheid selecteren voor je deze product kan opslaan!")
            '   MessageBox.Show("Je moet minstens een gelegenheid selecteren voor je deze product kan opslaan!", "FOUT", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            errProvider.SetError(chkGelegenheid, "")
        End If

        'double validation..
        If isOk Then
            Dim dict As Dictionary(Of String, Boolean) = Validation.ValidateProduct(txtNaam.Text, txtOmschrijving.Text, txtPrijs.Text)
            If dict.Item("isValid") Then
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                If dict.Item("txtNaam") = False Then
                    errProvider.SetError(txtNaam, "Het productnaam moet minstens 3 karakters zijn.")
                Else
                    errProvider.SetError(txtNaam, "")
                End If
                If dict.Item("txtOmschrijving") = False Then
                    errProvider.SetError(txtOmschrijving, "Het product omschrijving moet minstens 10 karakters zijn.")
                Else
                    errProvider.SetError(txtOmschrijving, "")
                End If
                If dict.Item("txtPrijs") = False Then
                    errProvider.SetError(txtPrijs, "Het prijs moet een nummer zijn en mag niet 0 of kleiner zijn.")
                Else
                    errProvider.SetError(txtPrijs, "")
                End If
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ' User clicked the Cancel button
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    'NOT WORKING- stack overflow
    'The e is a IntemCheckEvent Args
    ' Private Sub chkGelegenheid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As ItemCheckEventArgs) Handles chkGelegenheid.ItemCheck
    '     EnableDisableControl(e)
    'End Sub

    Private Sub txtNaam_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNaam.TextChanged, txtOmschrijving.TextChanged, txtPrijs.TextChanged, chkGelegenheid.ItemCheck
        EnableDisableControl()
        If sender.Equals(txtPrijs) Then

            If Not IsNumeric(txtPrijs.Text) Then
                Console.WriteLine("the price must be numeric!")
                btnOk.Enabled = False
            Else
                '   txtPrijs.Text = CStr(Math.Round(CDec(txtPrijs.Text), 2))
            End If

            txtPrijs.Text = txtPrijs.Text.Replace(".", ",")
            txtPrijs.Select(txtPrijs.Text.Length, 0)
        End If

    End Sub
#End Region

    Private Sub btnSearchPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchPic.Click
        ofd.Filter = "Images (*.jpg)|*.jpg|Gif (*.gif)|*.gif"
        ofd.Title = "Kies een afbeelding"

        Dim settingsPath As String = My.Application.Info.DirectoryPath & "\settings.xml"
        Dim settings = XDocument.Load(settingsPath)
        Dim imagesFolder As String = settings.<settings>.<imagesFolder>.Value
        'get the settings from the 

        ofd.InitialDirectory = CStr(IIf(imagesFolder <> "none", imagesFolder, My.Application.Info.DirectoryPath))

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim imageFileName As String = System.IO.Path.GetFileName(ofd.FileName)
            If System.IO.File.Exists(ofd.FileName) = True Then
                System.IO.File.Copy(ofd.FileName, imagesFolder & "\" & imageFileName, True)
            End If
            txtAfbeelding.Text = "./" & imageFileName
        End If
    End Sub

    Private Sub EnableDisableControl()
        'the reason I do this is because the checkedlistbox only sets the itemcheck AFTER you have SELECTED something again!
        'Found no workaround... 
        If txtNaam.Text = "" Or txtNaam.Text = "" Or txtOmschrijving.Text = "" Then ' Or chkGelegenheid.CheckedItems.Count = 0 Then
            btnOk.Enabled = False
        Else
            btnOk.Enabled = True
        End If
    End Sub


    Private Sub LoadOcasions()
        Dim gelegenheidList As List(Of Integer) = Product.GetGelegenheidForProduct(Product(), frmMain.dbh)

        Occasions = Occasion.SelectAllOccasions(frmMain.dbh)
        For Each item As Occasion In Occasions
            chkGelegenheid.Items.Add(item)
            For Each gelegenheidId As Integer In gelegenheidList
                If item.Id = gelegenheidId Then
                    Dim i As Integer = chkGelegenheid.FindString(item.toString)
                    chkGelegenheid.SetItemChecked(i, True)
                End If
            Next
        Next
    End Sub


    Private Sub btnAddOccasion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddOccasion.Click
        chkGelegenheid.Items.Clear()
        Dim occName As String = ""
        occName = InputBox("Nieuwe gelegenheid toevoegen", "Gelegenheid toevoegen")
        If occName <> "" Then
            Occasion.AddOccasion(frmMain.dbh, New Occasion(0, occName))
            LoadOcasions()
        End If
    End Sub

    Private Sub txtAfbeelding_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAfbeelding.TextChanged

    End Sub
End Class